<?php

namespace App\Http\Middleware;

use App\Http\Controllers\Utils\ResponseFactory;
use Closure;
use Illuminate\Http\Request;

class VerifyInputParams
{
    public  $IllegaChars = [
        '>',
        '<',
        '\\',
        '*',
        '#'
    ];

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\JsonResponse
     */
    public function handle(Request $request, Closure $next)
    {

        $params = $request->all();

        if (count($params)>15){
            return response()->json([
               "code" => 412,
               "msg"  => "参数过多"
            ],412);
        }
//        foreach ($params as $key => $value){
//            foreach ($this->IllegaChars as $IlleageChar){
//                if (is_string($value)){
//                    if (strpos($value, $IlleageChar) !== false){
//                        return response()->json([
//                            "code" => 412,
//                            "msg"  => "{$key}的值中含有违法字符"
//                        ],412);
//                    }
//                }
//
//            }
//        }
        $this->deepCheck($params);
        return $next($request);
    }

    public function deepCheck($params){
        echo count($params) . "\n";
        foreach ($params as $key => $value){
            foreach ($this->IllegaChars as $IlleageChar){
                if (is_string($value)){
                    if (strpos($value, $IlleageChar) !== false){
//                        return response()->json([
//                            "code" => 412,
//                            "msg"  => "{$key}的值中含有违法字符"
//                        ],412);

                    }
                    echo $key . " = " . $value . " " ."\n";
                }
                if (is_array($value)){
                    $this->deepCheck($value);
                }

            }
        }
    }
}
